package com.ruoyi.client.controller;

import com.ruoyi.activity.model.domain.User;
import com.ruoyi.client.model.dto.ApiWorkRequest;
import com.ruoyi.activity.service.IUserService;
import com.ruoyi.activity.service.IWorkService;
import com.ruoyi.client.vo.UserLoginVO;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.base.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 作品信息Controller
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/api/work")
public class ApiWorkController extends BaseController {
    @Autowired
    private IWorkService workService;

    @Autowired
    private IUserService userService;

    /**
     * 报名
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ApiWorkRequest apiWorkRequest, HttpServletRequest request) {
        UserLoginVO loginUserVO = userService.getUserLoginVO(request);
        if (loginUserVO == null) {
            return null;
        }
        String identity = loginUserVO.getIdentity();
        if (identity != null && !"教师".equals(identity)) {
            throw new BaseException("你没有权限提交作品");
        }
        boolean result = workService.joinWork(apiWorkRequest,loginUserVO);
        return AjaxResult.success(result);
    }

}
